package com.shujia.mr.kqzl;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.ArrayList;

class KQZLMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
    private ArrayList<String> ids = new ArrayList<>();

    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context) throws IOException, InterruptedException {
        String line = value.toString();
        /*
            3种情况：
                1、标题
                2、正常的检测数据
                3、<html> 页面未找到
         */
        if (line.startsWith("date")) {
            String[] infos = line.split(",");
            //这一行是标题
            for (int i = 3; i < infos.length; i++) {
                ids.add(infos[i].substring(0,infos[i].length()-1));
            }
        } else if (line.startsWith("2018") && line.contains("PM2.5")) {
            String[] infos = line.split(",");
            for (int i = 3, j = 0; i < infos.length && j < ids.size(); i++, j++) {
                if("".equals(infos[i]) || " ".equals(infos[i])){
                    infos[i] = "0";
                }
                context.write(new Text(infos[0]+"-"+ids.get(j)),new LongWritable(Long.parseLong(infos[i])));
            }
        }
    }
}

class KQZLReducer extends Reducer<Text, LongWritable, Text, LongWritable>{
    @Override
    protected void reduce(Text key, Iterable<LongWritable> values, Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {
        long sum = 0;
        long count = 0;
        for (LongWritable value : values) {
            sum+=value.get();
            count++;
        }
        context.write(key, new LongWritable(sum/count));
    }
}

public class KQZLDemo {
    public static void main(String[] args) throws Exception{
        Configuration conf = new Configuration();

        conf.set("fs.defaultFS", "hdfs://master:9000");

        Job job = Job.getInstance(conf);
        // 设置reduce的个数
//        job.setNumReduceTasks(4);

        job.setJarByClass(KQZLDemo.class);

        job.setJobName("监测点日平均PM2.5值");

        job.setMapperClass(KQZLMapper.class);

        job.setReducerClass(KQZLReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));

        boolean b = job.waitForCompletion(true);
        if(b){
            System.out.println("32期 监测点日平均PM2.5值mapreduce实现执行成功！>_-");
        }else {
            System.out.println("32期 监测点日平均PM2.5值mapreduce实现执行失败！T_T");
        }
    }
}
